﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ex_uppgift
{
    public static class Serializer
    {

        public static void SerializeContacts()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Contact>));
            TextWriter textWriter = new StreamWriter(Environment.SpecialFolder.MyDocuments + "contacts.xml");
            serializer.Serialize(textWriter, Form1.contacts);
            textWriter.Close();
        }

        public static void DeserializeContacts()
        {

            if (File.Exists(Environment.SpecialFolder.MyDocuments + "contacts.xml"))
            {
                XmlSerializer readserializer = new XmlSerializer(typeof(List<Contact>));
                TextReader textReader = new StreamReader(Environment.SpecialFolder.MyDocuments + "contacts.xml");

                Form1.contacts = (List<Contact>)readserializer.Deserialize(textReader);

                textReader.Close();

                
                
            }

            CRM_System.form.RemoveButtonVisibiltity();
        }

        public static void SerializeRelative()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Relative>));
            TextWriter textWriter = new StreamWriter(Environment.SpecialFolder.MyDocuments + "relatives.xml");
            serializer.Serialize(textWriter, Form1.relatives);
            textWriter.Close();
        }

        public static void DeserializeRelatives()
        {

            if (File.Exists(Environment.SpecialFolder.MyDocuments + "relatives.xml"))
            {
                XmlSerializer readserializer = new XmlSerializer(typeof(List<Relative>));
                TextReader textReader = new StreamReader(Environment.SpecialFolder.MyDocuments + "relatives.xml");

                Form1.relatives = (List<Relative>)readserializer.Deserialize(textReader);
                textReader.Close();
            }
        }
    }
}
